home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
rjs.lha
/
RJS
/
Transport
/
src
/
UnixSocket.C
< prev
Wrap
C/C++ Source or Header
|
1991-06-14
|
2KB
|
105 lines
#include "Transport.h"
#include <stdio.h>
#include <osfcn.h>
#include <libc.h>
#include <string.h>
//#include <iostream.h>
#include <RJS/Util.h>
int RJS_UnixSocket::active(const RJS_UnixAddress &ua)
{
sex=Active;
if (inuse()) close();
if (!socket() || !connect(ua,ua.addrlen)) {
if (return_on_error) return 0;
RJS_Util::crash_and_burn("RJS_UnixSocket","active",sys_errlist[errno]);
} else {
ss_set(SS_success);
return 1;
}
}
int RJS_UnixSocket::passive(const RJS_UnixAddress &ua)
{
sex=Passive;
if (inuse()) close();
if (!socket() || !bind(ua,ua.addrlen)|| !listen(SOMAXCONN)) {
if (return_on_error) return 0;
RJS_Util::crash_and_burn("RJS_UnixSocket","open",sys_errlist[errno]);
} else {
unlink_socket=1;
filename=ua.pathname();
ss_set(SS_success);
return 1;
}
}
RJS_UnixSocket::~RJS_UnixSocket()
{
if (inuse()) close();
}
int RJS_UnixSocket::close()
{
if ( unlink_socket) {
unlink(filename);
}
int s = ::close(td);
if (s==-1) {
ss_set(RJS_Status(errno));
if (return_on_error) return 0;
RJS_Util::crash_and_burn("RJS_UnixSocket","close",ss_message());
}
ss_set(SS_success);
td = -1;
return 1;
}
int RJS_UnixSocket::accept(RJS_UnixSocket &newsocket)
{
if (!ok() || !is_passive()) {
newsocket.td = -1;
newsocket.ss_set(SS_error);
return 0;
}
if (newsocket.inuse()) newsocket.close();
newsocket = *this;
RJS_UnixAddress remote;
remote.addrlen=remote.size();
newsocket.td = RJS_Socket::accept(remote,remote.addrlen);
if (newsocket.td < 0 ) {
ss_set(RJS_Status(errno));
if (return_on_error) return 0;
RJS_Util::crash_and_burn("RJS_UnixSocket","accept",ss_message());
} else {
ss_set(SS_success);
return 1;
}
}
int RJS_UnixSocket::getpeername(RJS_UnixAddress &ua)
{
ua.addrlen=ua.size();
int stat=RJS_Socket::getpeername(ua,ua.addrlen);
return stat;
}
int RJS_UnixSocket::getsockname(RJS_UnixAddress &ua)
{
ua.addrlen=ua.size();
int stat=RJS_Socket::getsockname(ua,ua.addrlen);
return stat;
}